package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.OwnerSelectDealPeopleAdapter;
import com.renwei.yunlong.adapter.ServiceSelectDealPeopleAdapter;
import com.renwei.yunlong.adapter.ServiceSelectProcessingUnitAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerDealPeopleBean;
import com.renwei.yunlong.bean.ServiceDealPeopleBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择处理人--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectDealPeopleActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    private Intent intent;

    private ACache mCache;

    private ServiceSelectDealPeopleAdapter serviceAdapter;
    private OwnerSelectDealPeopleAdapter ownerAdapter;

    String companyType;
    LoginBean loginBean;
    ServiceLoginBean serviceBean;
    String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_sponsor);
        ButterKnife.bind(this);
        mCache = ACache.get(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择处理人");
        rlSearch.setOnClickListener(this);

        companyType = mCache.getAsString("companyType");
        loginBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        content = etSearch.getText().toString().trim();
        if ("1".equals(companyType)) {//业主
            if (loginBean != null) {
                //{"ownerCode":公司code，“name”:搜索框查询条件}
                String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",name:\"" + "" + "\"}";
                OwnerSelectDealPeopleHttp(jsonBean);
            }
        } else if ("2".equals(companyType)) {//服务商
            if (serviceBean != null) {
                //{"serviceProviderCode":公司code，"engineerStatus":1,“name”:搜索框查询条件}
                String jsonBean = "{serviceProviderCode:\"" + serviceBean.getRows().getServiceProviderCode() + "\",engineerStatus:\"" + "1" + "\",name:\"" + "" + "\"}";
                serviceSelectDealPeopleHttp(jsonBean);
            }
        }
    }

    //服务商工单处理人
    private void serviceSelectDealPeopleHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectDealPeopleActivity.this);
        hp.setUrl(Api.SERVICE_QUERYSYNEMPLOyYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主工单处理人
    private void OwnerSelectDealPeopleHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectDealPeopleActivity.this);
        hp.setUrl(Api.OWNER_QUERYASSIGN_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //业主适配器数据
    private void setsOwnerListDate(List<OwnerDealPeopleBean.Rows> mList){

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ownerAdapter = new OwnerSelectDealPeopleAdapter(SelectDealPeopleActivity.this, mList);
        ownerAdapter.setOnItemClickListener(new OwnerSelectDealPeopleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name,String employeeId) {
                intent = new Intent();
                intent.putExtra("name",name);
                mCache.put("employeeId",employeeId);
                setResult(RESULT_OK, intent);
                SelectDealPeopleActivity.this.finish();
            }
        });
        recyclerView.setAdapter(ownerAdapter);
    }

    //服务商适配器数据
    private void setsServiceListDate(List<ServiceDealPeopleBean.Rows> mList){
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        serviceAdapter = new ServiceSelectDealPeopleAdapter(SelectDealPeopleActivity.this, mList);
        serviceAdapter.setOnItemClickListener(new ServiceSelectDealPeopleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name,String serviceEmployeeId) {
                intent = new Intent();
                intent.putExtra("name",name);
                mCache.put("serviceEmployeeId",serviceEmployeeId);
                setResult(RESULT_OK, intent);
                SelectDealPeopleActivity.this.finish();
            }
        });
        recyclerView.setAdapter(serviceAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_search:
                if ("1".equals(companyType)) {//业主
                    if (loginBean != null) {
                        //{"ownerCode":公司code，“name”:搜索框查询条件}
                        String jsonBean = "{ownerCode:\"" + loginBean.getRows().getOwnerCode() + "\",name:\"" + content + "\"}";
                        OwnerSelectDealPeopleHttp(jsonBean);
                    }
                } else if ("2".equals(companyType)) {//服务商
                    if (serviceBean != null) {
                        //{"serviceProviderCode":公司code，"engineerStatus":1,“name”:搜索框查询条件}
                        String jsonBean = "{serviceProviderCode:\"" + serviceBean.getRows().getServiceProviderCode() + "\",engineerStatus:\"" + "1" + "\",name:\"" + content + "\"}";
                        serviceSelectDealPeopleHttp(jsonBean);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1://业主
                Log.e("业主选择处理人返回",result);
                OwnerDealPeopleBean ownerDealPeopleBean = new Gson().fromJson(result,OwnerDealPeopleBean.class);
                if (ownerDealPeopleBean.getMessage().getCode() == 200){
                    setsOwnerListDate(ownerDealPeopleBean.getRows());
                }
                break;
            case 2://服务商
                ServiceDealPeopleBean serviceDealPeopleBean = new Gson().fromJson(result, ServiceDealPeopleBean.class);
                if (serviceDealPeopleBean.getMessage().getCode() == 200) {
                    setsServiceListDate(serviceDealPeopleBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
